package com.genesis.yunnanji.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.genesis.yunnanji.R;
import com.genesis.yunnanji.activity.Specialty;
import com.genesis.yunnanji.activity.WebActivity;
import com.genesis.yunnanji.adapter.MushRoomAdapter;
import com.genesis.yunnanji.adapter.MushRoomHorizontal;
import com.genesis.yunnanji.bean.MushRoomBean;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.genesis.yunnanji.utils.GenesisUtils;
import com.genesis.yunnanji.utils.NetWorkUtils;
import com.google.gson.Gson;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;
import com.yqritc.recyclerviewflexibledivider.VerticalDividerItemDecoration;

import org.json.JSONObject;
import org.xutils.http.RequestParams;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/4/17.
 */

public class MushRoom extends Fragment {

    private View view;
    private GenesisUtils genesisUtils;
    private RecyclerView recyclerView1;
    private RecyclerView recyclerView2;
    private TwinklingRefreshLayout refreshLayout;
    private int page = 1;
    private Intent intent;
    private String connectUrl;
    private MushRoomBean mushRoomBean;
    private List<MushRoomBean.ResultBean.SpelistBean> arraySpelist = new ArrayList<>();
    private List<MushRoomBean.ResultBean.ListBean> arrayNewslist = new ArrayList<>();
    private MushRoomAdapter mushRoomAdapter;
    private MushRoomHorizontal roomHorizontalAdapter;
    private int cate;


    public static MushRoom newInstance(int param) {
        MushRoom obj = new MushRoom();
        Bundle bundle = new Bundle();
        bundle.putInt("args1", param);
        obj.setArguments(bundle);
        return obj;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = this.getActivity().getLayoutInflater().inflate(R.layout.fragment_mushroom, null);
            recyclerView1 = (RecyclerView) view.findViewById(R.id.rv_mushroom_rv1);
            recyclerView2 = (RecyclerView) view.findViewById(R.id.rv_mushroom_rv2);
            refreshLayout = (TwinklingRefreshLayout) view.findViewById(R.id.swipe_mushroom_refresh);
            genesisUtils = GenesisUtils.getIntance();
            if (getArguments() != null) {
                cate = getArguments().getInt("args1");
                if (getArguments().getInt("args1") == 1) {
                    connectUrl = GenesisApiConfig.TECHAN;
                } else if (getArguments().getInt("args1") == 2) {
                    connectUrl = GenesisApiConfig.JUNKUN;
                } else if (getArguments().getInt("args1") == 3) {
                    connectUrl = GenesisApiConfig.FEIYI;
                }
            }
            getNewsData(refreshLayout);
            initView();
        } else {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null) {
                parent.removeView(view);
            }
        }

        return view;
    }

    protected void initView() {

//        LoadingView loadingView = new LoadingView(this.getActivity());
//        refreshLayout.setBottomView(loadingView);

        refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                page = 1;
                arraySpelist.clear();
                arrayNewslist.clear();
                getNewsData(refreshLayout);
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                page += 1;
                getNewsData(refreshLayout);
            }
        });


    }

    private View getFooterView() {
        RelativeLayout bottonview;
        View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_bottonview, (ViewGroup) recyclerView1.getParent(), false);
        bottonview = (RelativeLayout) view.findViewById(R.id.view_bottonview);
        bottonview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cate == 1) {
                    intent = new Intent(getActivity(), Specialty.class);
                    intent.putExtra("title", "云品特产");
                    intent.putExtra("id", "1");
                    startActivity(intent);
                } else if (cate == 2) {
                    intent = new Intent(getActivity(), Specialty.class);
                    intent.putExtra("title", "云南菌库");
                    intent.putExtra("id", "2");
                    startActivity(intent);
                } else if (cate == 3) {
                    intent = new Intent(getActivity(), Specialty.class);
                    intent.putExtra("title", "云南非遗");
                    intent.putExtra("id", "15");
                    startActivity(intent);
                }
            }
        });
        return view;
    }

    private void getNewsData(final TwinklingRefreshLayout refreshLayout) {
        final RequestParams params = new RequestParams(GenesisApiConfig.HOST + connectUrl);
        params.addBodyParameter("p", page + "");
        NetWorkUtils.doCacheGet(params, new NetWorkUtils.HttpResultListener() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                Log.e("data", jsonObject.toString());
                Gson gson = new Gson();
                mushRoomBean = gson.fromJson(jsonObject.toString(), MushRoomBean.class);

                if (page == 1) {
                    arraySpelist.addAll(mushRoomBean.getResult().getSpelist());
                    arrayNewslist.addAll(mushRoomBean.getResult().getList());
                    mushRoomAdapter = new MushRoomAdapter(arrayNewslist, getActivity(), false);
                    recyclerView2.setLayoutManager(new LinearLayoutManager(getActivity()));
                    recyclerView2.addItemDecoration(new HorizontalDividerItemDecoration.Builder(getActivity()).color(getResources().getColor(R.color.divider)).build());
                    mushRoomAdapter.addHeaderView(getHeaderView());
                    recyclerView2.setAdapter(mushRoomAdapter);
                    mushRoomAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                            intent = new Intent(getActivity(), WebActivity.class);
                            if (genesisUtils.isLogin()) {
                                intent.putExtra("url", arrayNewslist.get(position).getTaget_link() + GenesisApiConfig.FROM_APP + GenesisApiConfig.APP_TOKEN + genesisUtils.getAppToken());
                            } else {
                                intent.putExtra("url", arrayNewslist.get(position).getTaget_link() + GenesisApiConfig.FROM_APP);

                            }
                            startActivity(intent);
                        }
                    });

                } else {
                    arrayNewslist.addAll(mushRoomBean.getResult().getList());
                }


            }

            @Override
            public void OnCached(JSONObject jsonObject) {
            }

            @Override
            public void onFinished() {
                if (refreshLayout != null) {
                    refreshLayout.finishRefreshing();
                    refreshLayout.finishLoadmore();
                }
            }

            @Override
            public void onError(String errorStr) {

            }
        });
    }

    View headerView;

    private View getHeaderView() {

        headerView = LayoutInflater.from(getContext()).inflate(R.layout.layout_spec_header, (ViewGroup) recyclerView2.getParent(), false);
        recyclerView1 = (RecyclerView) headerView.findViewById(R.id.rv_mushroom_rv1);
        roomHorizontalAdapter = new MushRoomHorizontal(arraySpelist, getActivity());
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recyclerView1.setLayoutManager(layoutManager);
        recyclerView1.addItemDecoration(new VerticalDividerItemDecoration.Builder(getActivity()).color(getResources().getColor(R.color.white)).size((int) getResources().getDimension(R.dimen.x7)).build());
        recyclerView1.setAdapter(roomHorizontalAdapter);
        roomHorizontalAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                intent = new Intent(getActivity(), WebActivity.class);
                if (genesisUtils.isLogin()) {
                    intent.putExtra("url", arraySpelist.get(position).getTaget_link() + GenesisApiConfig.FROM_APP + GenesisApiConfig.APP_TOKEN + genesisUtils.getAppToken());
                } else {
                    intent.putExtra("url", arraySpelist.get(position).getTaget_link() + GenesisApiConfig.FROM_APP);

                }
                startActivity(intent);
            }
        });
        roomHorizontalAdapter.addFooterView(getFooterView());

        return headerView;
    }

}
